package com.anyou.mall.bucket.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.bucket.dto.BucketAuditDto;
import com.anyou.mall.bucket.vo.BucketAuditVo;
import com.anyou.mall.bucket.vo.BucketAuditVo2;
import com.anyou.mall.platform.domain.OrdDelivery;
import com.anyou.mall.platform.mapper.OrdDeliveryMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.bucket.mapper.ReturnBucketAuditMapper;
import com.anyou.mall.bucket.domain.ReturnBucketAudit;
import com.anyou.mall.bucket.service.IReturnBucketAuditService;

/**
 * 退还桶押金审核Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-11
 */
@Service
public class ReturnBucketAuditServiceImpl implements IReturnBucketAuditService
{
    @Autowired
    private ReturnBucketAuditMapper returnBucketAuditMapper;
    @Autowired
    private OrdDeliveryMapper ordDeliveryMapper;


    /**
     * 查询退还桶押金审核
     *
     * @param returnBucketAuditId 退还桶押金审核主键
     * @return 退还桶押金审核
     */
    @Override
    public ReturnBucketAudit selectReturnBucketAuditByReturnBucketAuditId(Long returnBucketAuditId)
    {
        return returnBucketAuditMapper.selectReturnBucketAuditByReturnBucketAuditId(returnBucketAuditId);
    }

    /**
     * 查询退还桶押金审核列表
     *
     * @param returnBucketAudit 退还桶押金审核
     * @return 退还桶押金审核
     */
    @Override
    public List<ReturnBucketAudit> selectReturnBucketAuditList(ReturnBucketAudit returnBucketAudit)
    {
        return returnBucketAuditMapper.selectReturnBucketAuditList(returnBucketAudit);
    }

    /**
     * 新增退还桶押金审核
     *
     * @param returnBucketAudit 退还桶押金审核
     * @return 结果
     */
    @Override
    public int insertReturnBucketAudit(ReturnBucketAudit returnBucketAudit)
    {
        returnBucketAudit.setCreateTime(DateUtils.getNowDate());
        return returnBucketAuditMapper.insertReturnBucketAudit(returnBucketAudit);
    }

    /**
     * 修改退还桶押金审核
     *
     * @param returnBucketAudit 退还桶押金审核
     * @return 结果
     */
    @Override
    public int updateReturnBucketAudit(ReturnBucketAudit returnBucketAudit)
    {
        returnBucketAudit.setUpdateTime(DateUtils.getNowDate());
        return returnBucketAuditMapper.updateReturnBucketAudit(returnBucketAudit);
    }

    /**
     * 批量删除退还桶押金审核
     *
     * @param returnBucketAuditIds 需要删除的退还桶押金审核主键
     * @return 结果
     */
    @Override
    public int deleteReturnBucketAuditByReturnBucketAuditIds(Long[] returnBucketAuditIds)
    {
        return returnBucketAuditMapper.deleteReturnBucketAuditByReturnBucketAuditIds(returnBucketAuditIds);
    }

    /**
     * 删除退还桶押金审核信息
     *
     * @param returnBucketAuditId 退还桶押金审核主键
     * @return 结果
     */
    @Override
    public int deleteReturnBucketAuditByReturnBucketAuditId(Long returnBucketAuditId)
    {
        return returnBucketAuditMapper.deleteReturnBucketAuditByReturnBucketAuditId(returnBucketAuditId);
    }

    /**
     * 退还桶押金申请列表
     * @param param
     * @return
     */
    @Override
    public List<BucketAuditVo> selectBucketAuditList(BucketAuditDto param) {
        return returnBucketAuditMapper.selectBucketAuditList(param);
    }
    /**
     * 退还桶押金详情
     * @param returnBucketAuditId
     * @return
     */
    @Override
    public BucketAuditVo2 selectBucketInfo(Long returnBucketAuditId) {
        BucketAuditVo2 bv=returnBucketAuditMapper.selectBucketInfo(returnBucketAuditId);
        List<OrdDelivery> ordDeliveries = ordDeliveryMapper.selectList(new LambdaQueryWrapper<OrdDelivery>());
        //查询桶押金
        BigDecimal yj = new BigDecimal(0);
        if(CollUtil.isNotEmpty(ordDeliveries)){
            yj=ordDeliveries.get(0).getBucketDetainPrice();
        }
        bv.setBucketDetainPrice(yj);
        //可退的桶押金的桶数量
        int num=bv.getDetainBucket()-bv.getOweBucket();
        //可退的桶押金总金额
        BigDecimal aa=new BigDecimal(num).multiply(yj);
        bv.setReturnPrice(num+"*"+yj+"元="+aa+"元");
        return bv;
    }
}
